package com.quanyan.user.serviceFacade.impl;

import com.quanyan.api.APIResponse;
import com.quanyan.common.constants.Constants;
import com.quanyan.common.vo.resp.RespCity;
import com.quanyan.user.mapper.TbCityMapper;
import com.quanyan.user.model.db.TbCity;
import com.quanyan.user.model.db.TbUser;
import com.quanyan.user.request.*;
import com.quanyan.user.response.RespOpenUserInterest;
import com.quanyan.user.response.RespUserInfoBase;
import com.quanyan.user.response.RespUserInfoDetail;
import com.quanyan.user.service.*;
import com.quanyan.user.serviceFacade.UserServiceFacade;
import com.quanyan.user.utils.LarkCodeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/6/12.
 */
@Service("userServiceFacade")
public class UserServiceFacadeImpl implements UserServiceFacade {
    private static final Logger logger = LoggerFactory.getLogger(UserServiceFacadeImpl.class);

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private UserQueryService userQueryService;

    @Autowired
    private UserInterestService userInterestService;

    @Autowired
    private UserInformationService userInformationService;

    @Autowired
    private UserLoginService userLoginService;

    @Autowired
    private TbCityMapper tbCityMapper;

    @Override
    public APIResponse<RespUserInfoBase> queryUserInfo(Integer uid) {
        return userQueryService.queryUserInfo(uid);
    }

    @Override
    public APIResponse<RespUserInfoBase> queryUserInfoByPhone(String mobile) {
            return userQueryService.queryUserInfo(mobile);
    }

    @Override
    public APIResponse<RespUserInfoBase> queryUserInfoByToken(String token) {
        return userInfoService.queryUserInfoByToken(token);
    }

    @Override
    public APIResponse updateUserInfo(ReqUserParam reqUserParam) {
        return userInfoService.updateUserInfo(reqUserParam);
    }

    @Override
    public APIResponse<RespUserInfoBase> queryUserInfoByWxOpenId(String wxOpenId) {
        return userQueryService.queryUserInfoByWxOpenId(wxOpenId);
    }

    @Override
    public APIResponse<List<RespUserInfoBase>> batchQueryUserInfo(List<Integer> lstUserId) {
        return userQueryService.batchQueryUserInfo(lstUserId);
    }

    @Override
    public APIResponse<RespUserInfoBase> appLogin(ReqAppUserLogin reqAppUserLogin) {
        return userInfoService.appLogin(reqAppUserLogin);
    }

    @Override
    public Boolean isBindingMobile(Integer uid) {
        return userInfoService.isBindingMobile(uid);
    }

    @Override
    public Integer getUserIdByToken(String token) {
        return userInfoService.getUserIdByToken(token);
    }

    @Override
    public Map<Integer, RespUserInfoBase> batchQueryUserInfoToMap(List<Integer> lstUserId) {
        return userQueryService.batchQueryUserInfoToMap(lstUserId);
    }
    
    /**
     * 获取用户二维码
     * @param uid 用户ID
     * @return 用户二维码
     */
    @Override
    public String getUserQRCode(Integer uid) {
        return userInfoService.getUserQRCode(uid);
    }

    @Override
    public APIResponse registerMemberUser(ReqBackstageUser reqBackstageUser) {
        return userInfoService.registerMemberUser(reqBackstageUser);
    }


    @Override
    public APIResponse batchRegisterMemberUser(List<ReqBackstageUser> reqBackstageUsers) {
        return userInfoService.batchRegisterMemberUser(reqBackstageUsers);
    }

    @Override
    public APIResponse<RespUserInfoDetail> queryUserInfoDetail(Integer userId) {
        return userQueryService.queryUserInfoDetail(userId);
    }

    @Override
    public APIResponse<List<RespUserInfoDetail>> batchQueryUserInfoDetail(List<Integer> lstUserId) {
        return userQueryService.batchQueryUserInfoDetail(lstUserId);
    }

    /**
     * 批量查询用户兴趣
     * @param uids
     * @return
     */
    public Map<Integer,List<RespOpenUserInterest>> batchQueryUserInterest(List<Integer> uids){
        return userInterestService.batchQueryUserInterest(uids);
    }

    @Override
    public APIResponse<RespUserInfoBase> queryUserInfo(Integer uid, Integer friendUid) {
        return userQueryService.queryUserInfo(uid,friendUid);
    }

    @Override
    public APIResponse<RespUserInfoDetail> queryUserInfoDetail(Integer uid, Integer friendUid) {
        return userQueryService.queryUserInfoDetail(uid,friendUid);
    }

    @Override
    public APIResponse<List<RespUserInfoBase>> batchQueryUserInfo(Integer uid, List<Integer> lstUserId) {
        return userQueryService.batchQueryUserInfo(uid,lstUserId);
    }

    @Override
    public APIResponse<List<RespUserInfoDetail>> batchQueryUserInfoDetail(Integer uid, List<Integer> lstUserId) {
        return userQueryService.batchQueryUserInfoDetail(uid,lstUserId);
    }

    /**
     * 更换用户手机号
     * @param reqChangePhone
     * @return
     */
    @Override
    public APIResponse changePhone(ReqChangePhone reqChangePhone) {
        return userInformationService.changePhone(reqChangePhone);
    }

    /**
     * 获取城市信息
     *
     * @param id 城市Id
     * @return
     */
    @Override
    public APIResponse<RespCity> getCity(Integer id) {
        if (id == null || id <= 0) {
            APIResponse.returnFail("请输入正确参数！");
        }

        RespCity respCity = new RespCity();
        try {
            TbCity tbCity = tbCityMapper.selectByPrimaryKey(id);
            if (tbCity != null) {
                respCity.setId(tbCity.getId());
                respCity.setName(tbCity.getName());
                respCity.setDesc(tbCity.getCityDesc());
                respCity.setPinyin(tbCity.getPinyin());
                respCity.setLat(tbCity.getLat());
                respCity.setLng(tbCity.getLng());
                respCity.setIsActive(tbCity.getIsActive());
            }
        } catch (Exception e) {
            logger.error("获取城市信息失败:{}", e.getMessage());
            return APIResponse.returnFail("获取城市信息失败");
        }

        return APIResponse.returnSuccess(respCity);
    }

    /**
     * 用户内部注册
     * @param reqInsideUserRegister 内部注册参数
     * @return RespUserInfoBase 用户基本信息
     */
    @Override
    public APIResponse<RespUserInfoBase> userInsideRegister(ReqInsideUserRegister reqInsideUserRegister) {
        String phone = reqInsideUserRegister.getPhone();
        String password = reqInsideUserRegister.getPassword();
        String appChannel = reqInsideUserRegister.getAppChannel();
        TbUser user;
        RespUserInfoBase respUserInfoBase = new RespUserInfoBase();
        user = userInfoService.getUser(phone);
        if (user == null) {
            logger.info("系统ID为：【{}】;注册用户的手机号：【{}】;注册渠道为：【{}】",reqInsideUserRegister.getSourceSystemId(),phone,appChannel);
            user = userLoginService.commonRegister(phone,password,appChannel, Constants.RegType.BACKSTAGE, LarkCodeUtil.generateDefaultNickName(phone),null,(byte)0,null);
            if (user != null) {
                respUserInfoBase.setUid(user.getId());
                respUserInfoBase.setNickName(user.getNickName());
                respUserInfoBase.setMobile(user.getMobile());
                respUserInfoBase.setIcon(user.getIcon());
                respUserInfoBase.setIsNew(0);//该用户是新用户
                return APIResponse.returnSuccess(respUserInfoBase);
            } else {
                return APIResponse.returnFail("用户注册失败，请稍后再试");
            }
        } else {
            respUserInfoBase.setUid(user.getId());
            respUserInfoBase.setNickName(user.getNickName());
            respUserInfoBase.setMobile(user.getMobile());
            respUserInfoBase.setIcon(user.getIcon());
            respUserInfoBase.setIsNew(1);//该用户不是新用户
            return APIResponse.returnSuccess(respUserInfoBase);
        }
    }
}
